'use strict';

module.exports = app => {
  const { STRING, INTEGER, DECIMAL, DATE, TEXT, ENUM, JSON } = app.Sequelize;

  const Clothing = app.model.define('clothing', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    user_id: {
      type: INTEGER,
      allowNull: false,
      comment: '用户ID',
    },
    category_id: {
      type: INTEGER,
      allowNull: false,
      comment: '分类ID',
    },
    name: {
      type: STRING(100),
      allowNull: false,
      comment: '衣服名称',
    },
    brand: {
      type: STRING(50),
      allowNull: true,
      comment: '品牌',
    },
    model: {
      type: STRING(50),
      allowNull: true,
      comment: '型号',
    },
    size: {
      type: STRING(20),
      allowNull: true,
      comment: '尺码',
    },
    color: {
      type: STRING(30),
      allowNull: true,
      comment: '颜色',
    },
    color_hex: {
      type: STRING(7),
      allowNull: true,
      comment: '颜色十六进制值',
    },
    material: {
      type: STRING(100),
      allowNull: true,
      comment: '材质',
    },
    price: {
      type: DECIMAL(10, 2),
      allowNull: true,
      comment: '价格',
    },
    purchase_date: {
      type: DATE,
      allowNull: true,
      comment: '购买日期',
    },
    purchase_place: {
      type: STRING(100),
      allowNull: true,
      comment: '购买地点',
    },
    season: {
      type: ENUM('spring', 'summer', 'autumn', 'winter', 'all'),
      allowNull: true,
      comment: '适用季节',
    },
    style: {
      type: ENUM('casual', 'formal', 'business', 'sport', 'party', 'vintage', 'street'),
      allowNull: true,
      comment: '风格',
    },
    occasion: {
      type: JSON,
      allowNull: true,
      comment: '适用场合',
    },
    images: {
      type: JSON,
      allowNull: true,
      comment: '图片URLs',
    },
    main_image: {
      type: STRING(255),
      allowNull: true,
      comment: '主图片URL',
    },
    status: {
      type: ENUM('available', 'wearing', 'washing', 'dry_cleaning', 'damaged', 'donated'),
      defaultValue: 'available',
      comment: '衣服状态',
    },
    wear_count: {
      type: INTEGER,
      defaultValue: 0,
      comment: '穿着次数',
    },
    last_worn_date: {
      type: DATE,
      allowNull: true,
      comment: '最后穿着日期',
    },
    care_instructions: {
      type: TEXT,
      allowNull: true,
      comment: '保养说明',
    },
    notes: {
      type: TEXT,
      allowNull: true,
      comment: '备注',
    },
    is_favorite: {
      type: app.Sequelize.BOOLEAN,
      defaultValue: false,
      comment: '是否收藏',
    },
    // 时间戳字段由Sequelize自动管理
  }, {
    tableName: 'clothing',
    comment: '衣服表',
    indexes: [
      {
        fields: ['user_id'],
      },
      {
        fields: ['category_id'],
      },
      {
        fields: ['color'],
      },
      {
        fields: ['season'],
      },
      {
        fields: ['style'],
      },
      {
        fields: ['status'],
      },
    ],
  });

  Clothing.associate = function() {
    // 衣服属于用户
    app.model.Clothing.belongsTo(app.model.User, {
      foreignKey: 'user_id',
      as: 'user',
    });

    // 衣服属于分类
    app.model.Clothing.belongsTo(app.model.Category, {
      foreignKey: 'category_id',
      as: 'category',
    });

    // 衣服可以有多个标签
    app.model.Clothing.belongsToMany(app.model.Tag, {
      through: app.model.ClothingTag,
      foreignKey: 'clothing_id',
      otherKey: 'tag_id',
      as: 'tags',
    });

    // 衣服可以在多个搭配中
    app.model.Clothing.belongsToMany(app.model.Outfit, {
      through: app.model.OutfitItem,
      foreignKey: 'clothing_id',
      otherKey: 'outfit_id',
      as: 'outfits',
    });
  };

  return Clothing;
};
